package cz.proks.easyhelpdesk.gae.api.impl.dbo;

import org.springframework.beans.BeanUtils;

import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.ConvertibleDbo;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.IdentifiableModelDbo;

public class ThreadMember extends cz.proks.easyhelpdesk.api.model.ThreadMember implements IdentifiableModelDbo, ConvertibleDbo<cz.proks.easyhelpdesk.api.model.ThreadMember>{
    private HelpdeskMember helpdeskMember;
    private Thread thread;
    private Integer ticketRead;
    
    @Override
    public Long getHelpdeskMemberId() {        
        return super.getHelpdeskMemberId();
    }

    @Override
    public void setHelpdeskMemberId(Long helpdeskMemberId) {
        super.setHelpdeskMemberId(helpdeskMemberId);
        
        if (helpdeskMemberId == null)
            helpdeskMember = null;
        else {
            helpdeskMember = new HelpdeskMember();      
            helpdeskMember.setId(helpdeskMemberId);
        }        
    }
    
    @Override
    public Long getThreadId() {        
        return super.getThreadId();
    }

    @Override
    public void setThreadId(Long threadId) {
        super.setThreadId(threadId);
        
        if (threadId == null)
            thread = null;
        else {
            thread = new Thread();      
            thread.setId(threadId);
        }        
    }
        
    @Override
    public boolean isRead() {
        return super.isRead();
    }

    @Override
    public void setRead(boolean read) {        
        super.setRead(read);
        ticketRead = read ? 1 : 0;
    }

    public HelpdeskMember getHelpdeskMember() {
        return helpdeskMember;
    }

    public void setHelpdeskMember(HelpdeskMember helpdeskMember) {
        this.helpdeskMember = helpdeskMember;
        super.setHelpdeskMemberId(helpdeskMember != null ? helpdeskMember.getId() : null);
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
        super.setThreadId(thread != null ? thread.getId() : null);
    }        

    public Integer getTicketRead() {
        return ticketRead;
    }

    public void setTicketRead(Integer ticketRead) {
        this.ticketRead = ticketRead;
        super.setRead(ticketRead == null || ticketRead.equals(0) ? false : true);
    }

    @Override
    public void toDbo(cz.proks.easyhelpdesk.api.model.ThreadMember domain) {
        BeanUtils.copyProperties(domain, this);
    }

    @Override
    public cz.proks.easyhelpdesk.api.model.ThreadMember asDomain() {
        cz.proks.easyhelpdesk.api.model.ThreadMember threadMember = new cz.proks.easyhelpdesk.api.model.ThreadMember();
        BeanUtils.copyProperties(this, threadMember, new String[] {"helpdeskMember", "thread"});        
        return threadMember;
    }    
}
